package com.zk.algorithm.array;

import com.zk.algorithm.Utils;
import com.zk.algorithm.annotation.Hard;
import com.zk.algorithm.annotation.LeetCodeExplore;

@LeetCodeExplore
@Hard
public class MaximumProductSubarray {

    public static void main(String...args) {
        Utils.println(new MaximumProductSubarray().maxProduct(new int[] {
                2,3,-2,4
        }));
    }

    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int max = nums[0];
        int min = nums[0];

        int result = max;

        for (int i = 1; i < nums.length; i++) {
            int A = max * nums[i];
            int B = min * nums[i];

            max = Math.max(A, Math.max(B, nums[i]));
            min = Math.min(A, Math.min(B, nums[i]));

            result = Math.max(max, result);
        }

        return result;
    }

}
